﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Model;
using Controller;

namespace Biffen
{
    public partial class MovieUI : Form
    {
        private List<Genre> genres = new List<Genre>();
        private List<Movie> movieSearchList = new List<Movie>();

        private MovieCtr movieCtr = new MovieCtr();

        private Movie currentMovieEdit = null;

        public MovieUI()
        {
            InitializeComponent();

            genres = this.movieCtr.GetAllGenres();
            List<String> list = new List<string>();

            foreach (Genre g in this.genres)
            {
                list.Add(g.Type);
            }

            checkedEditList.DataSource = list;
            checkedList.DataSource = list;
        }

        private void btnEditCreate_Click(object sender, EventArgs e)
        {
            // NEW MOVIE
            string title = this.titleText.Text;
            string instructor = this.instructorText.Text;
            string actors = this.actorsText.Text;
            string description = this.descText.Text;
            int duration = (int)this.durationNum.Value;

            List<string> gs = new List<string>();

            for (int i = 0; i < checkedList.CheckedItems.Count; i++)
            {
                //Console.WriteLine(checkedList.CheckedItems[i].ToString());
                gs.Add(checkedList.CheckedItems[i].ToString());
            }
            
            List<Genre> selectedGenres = new List<Genre>();

            foreach (Genre g in genres)
            {
                if (gs.Contains(g.Type))
                {
                    selectedGenres.Add(g);
                }
            }
            
            if (title.Length > 0 &&
                instructor.Length > 0 &&
                actors.Length > 0 &&
                description.Length > 0 &&
                duration > 0 && 
                selectedGenres.Count > 0
                )
            {
                Movie movie = new Movie(title, instructor, duration, description, 1, actors);
                
                if (movie != null)
                {
                    // Adding selected genres
                    movie.Genres = selectedGenres;

                    int result = movieCtr.CreateMovie(movie);
                    if (result > 0)
                    {
                        MessageBox.Show("Din film er blevet gemt.", "Film gemt");
                    }
                    else
                    {
                        MessageBox.Show("Der opstod en fejl. Din film blev ikke gemt!", "Fejl opstået!");
                    }
                }
                else
                {
                    MessageBox.Show("Kunne ikke oprette filmen!", "Fejl opstået!");
                }
            }
            else
            {
                MessageBox.Show("Du skal udfylde alle felter for at kunne gemme.", "Indtastningsfejl");
            }
        }

        private void DoSearch()
        {
            string searchTitle = searchEditText.Text;

            this.movieSearchList = this.movieCtr.FindMoviesByTitle(searchTitle);
            if (this.movieSearchList != null)
            {
                List<String> movieArray = new List<string>();

                if (this.movieSearchList.Count > 0)
                {

                    foreach (Movie m in this.movieSearchList)
                    {
                        movieArray.Add(m.Title + " (" + m.Instructor + ")");
                    }
                }
                else
                {
                    MessageBox.Show("Der blev ikke fundet nogle resultater ud fra din søgning. Prøv igen.", "Ingen resultater fundet");
                }

                this.movieList.DataSource = movieArray;
            }
        }

        private void searchBtn_Click(object sender, EventArgs e)
        {
            this.DoSearch();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // UPDATE MOVIE
            string title = this.titleEditText.Text;
            string instructor = this.instructorEditText.Text;
            string actors = this.actorsEditText.Text;
            string description = this.descEditText.Text;
            int duration = (int)this.durationEditVal.Value;

            List<string> gs = new List<string>();

            for (int i = 0; i < checkedEditList.CheckedItems.Count; i++)
            {
                //Console.WriteLine(checkedList.CheckedItems[i].ToString());
                gs.Add(checkedEditList.CheckedItems[i].ToString());
            }

            List<Genre> selectedGenres = new List<Genre>();

            foreach (Genre g in genres)
            {
                if (gs.Contains(g.Type))
                {
                    selectedGenres.Add(g);
                }
            }

            if (title.Length > 0 &&
                instructor.Length > 0 &&
                actors.Length > 0 &&
                description.Length > 0 &&
                duration > 0 &&
                selectedGenres.Count > 0
                )
            {
                
                //Movie movie = new Movie(title, instructor, duration, description, 1, actors);

                if (currentMovieEdit != null)
                {

                    // Updating info in object
                    currentMovieEdit.Title = title;
                    currentMovieEdit.Instructor = instructor;
                    currentMovieEdit.Actors = actors;
                    currentMovieEdit.Description = description;
                    currentMovieEdit.Duration = duration;

                    // Adding selected genres
                    currentMovieEdit.Genres = selectedGenres;

                    int result = movieCtr.UpdateMovie(currentMovieEdit);
                    if (result > 0)
                    {
                        MessageBox.Show("Din film er blevet opdateret.", "Film opdateret");
                    }
                    else
                    {
                        MessageBox.Show("Der opstod en fejl. Din film blev ikke opdateret!", "Fejl opstået!");
                    }
                }
                else
                {
                    MessageBox.Show("Kunne ikke oprette filmen!", "Fejl opstået!");
                }
            }
            else
            {
                MessageBox.Show("Du skal udfylde alle felter for at kunne gemme.", "Indtastningsfejl");
            }
        }

        private void searchEditText_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                this.DoSearch();
            }
        }

        private void movieList_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = this.movieList.SelectedIndex;
            currentMovieEdit = this.movieSearchList[index];

            this.titleEditText.Text = currentMovieEdit.Title;
            this.instructorEditText.Text = currentMovieEdit.Instructor;
            this.actorsEditText.Text = currentMovieEdit.Actors;
            this.durationEditVal.Value = currentMovieEdit.Duration;
            this.descEditText.Text = currentMovieEdit.Description;

            List<String> gs = new List<string>();
            foreach (Genre g in currentMovieEdit.Genres)
            {
                gs.Add(g.Type);
            }

            for (int i = 0; i < checkedEditList.Items.Count; i++)
            {
                if (gs.Contains(checkedList.Items[i]))
                {
                    checkedEditList.SetItemChecked(i, true);
                }
                else
                {
                    checkedEditList.SetItemChecked(i, false);
                }
            }

        }
    }
}
